package com.fenxiangle.yueding.feature.home.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.fenxiangle.yueding.R;
import com.fenxiangle.yueding.common.widget.dialog.ActionSheetDialog;
import com.fenxiangle.yueding.common.widget.dialog.OnOperItemClickL;
import com.fenxiangle.yueding.entity.bo.TabEntity;
import com.fenxiangle.yueding.entity.bo.UserCenterBo;
import com.fenxiangle.yueding.feature.login.view.LoginActivity;
import com.fenxiangle.yueding.feature.mine.contract.MineContract;
import com.fenxiangle.yueding.feature.mine.dependencies.mine.DaggerMineComponent;
import com.fenxiangle.yueding.feature.mine.dependencies.mine.MinePresenterModule;
import com.fenxiangle.yueding.feature.mine.view.PhotoFragment;
import com.fenxiangle.yueding.feature.mine.view.VedioOtherFragment;
import com.fenxiangle.yueding.feature.publish.view.PublishInviteActivity;
import com.fenxiangle.yueding.framework.DataServer;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class UserInfoActivity extends BaseActivity implements OnOperItemClickL, MineContract.View {

    @BindView(R.id.iv_mine_head)
    ImageView ivMineHead;
    @BindView(R.id.tl_6)
    CommonTabLayout tl6;
    @BindView(R.id.tv_user_name)
    TextView mTvUserName;
    @BindView(R.id.tv_user_id)
    TextView mTvUserId;
    @BindView(R.id.tv_user_fans)
    TextView mTvUserFans;
    @BindView(R.id.iv_user_sex)
    ImageView mIvUserSex;
    @BindView(R.id.tv_user_age)
    TextView mTvUserAge;
    @BindView(R.id.tv_user_certification_type)
    TextView mTvUserCertificationType;
    @BindView(R.id.tv_user_certification_position)
    TextView mTvUserCertificationPosition;
    @BindView(R.id.tv_user_address)
    TextView mTvUserAddress;
    @BindView(R.id.tv_user_time)
    TextView mTvUserTime;
    @BindView(R.id.tv_user_score)
    TextView mTvUserScore;
    @BindView(R.id.tv_user_comment)
    TextView mTvUserComment;
    @BindView(R.id.btrn_mine_setting)
    TextView mBtrnMineSetting;
    @BindView(R.id.btn_yaoyue)
    TextView mBtnYaoyue;
    private String[] mTitles = {"视频", "照片"};
    private ArrayList<CustomTabEntity> mTabEntities;

    private ArrayList<Fragment> mFragments;
    private ActionSheetDialog dialog;
    private List<LocalMedia> selectList;
    private String uri;

    private String mId;
    private UserCenterBo mData;
    private int isFollow;
    @Inject
    MineContract.Presenter mPresenter;

    @Override
    public int attachLayoutRes() {
        return R.layout.activity_user_info;
    }

    @Override
    protected void initInjector() {
        DaggerMineComponent.builder().minePresenterModule(new MinePresenterModule(this)).build().inject(this);
    }

    @Override
    protected void initView() {
        AM.image().bindToCircleObject(R.drawable.iv_user_head, ivMineHead);


        final String[] stringItems = {"拍照", "从手机相册选择"};
        dialog = new ActionSheetDialog(this, stringItems, null);
        dialog.isTitleShow(false);
        dialog.setOnOperItemClickL(this);
    }

    @Override
    public void onStart() {
        super.onStart();


    }


    @Override
    protected void initData() {
        mId = this.getIntent().getStringExtra("id");
        mPresenter.getOtherUserData(mId);
    }

    @Override
    protected void initEvent() {

    }


    @OnClick({R.id.iv_mine_head, R.id.btrn_mine_more, R.id.btrn_mine_setting, R.id.btrn_mine_back, R.id.btn_yaoyue, R.id.btn_view_rating})
    public void onViewClicked(View view) {
        if (!AM.user().isLogin()) {
            Intent intent = new Intent(this, LoginActivity.class);
            startActivity(intent);
            return;
        }
        switch (view.getId()) {
            case R.id.iv_mine_head:
                break;
            case R.id.btrn_mine_back:
                finish();
                break;
            case R.id.btrn_mine_more:
                break;
            case R.id.btn_yaoyue:
                Intent intent = new Intent(new Intent(this, PublishInviteActivity.class));
                intent.putExtra("uid", mData.getUserUid());
                intent.putExtra("pos", mData.getOccupation());
                startActivity(intent);
                break;
            case R.id.btrn_mine_setting:
                if (isFollow == 0) {
                    if (!TextUtils.isEmpty(mData.getUserUid())) {
                        mPresenter.attentionFollow(mData.getUserUid());
                    }
                } else {
                    if (!TextUtils.isEmpty(mData.getUserUid())) {
                        mPresenter.attentionUnFollow(mData.getUserUid());
                    }
                }

                break;
            case R.id.btn_view_rating:
                Intent intents = new Intent(new Intent(this, ViewRatingActivity.class));
                intents.putExtra("uid", mData.getUserUid());
                startActivity(intents);
                break;
        }
    }


    @Override
    public void onOperItemClick(AdapterView<?> parent, View view, int position, long id) {

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == -1) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:
                    // 图片选择结果回调
                    selectList = PictureSelector.obtainMultipleResult(data);
                    for (int i = 0; i < selectList.size(); i++) {
                        uri = selectList.get(i).getCutPath();
                    }
                    mPresenter.uploadUserHead(uri);
                    break;
            }
        }
    }


    @Override
    public void showUserDataSuccess(UserCenterBo userCenterBo) {
        isFollow = userCenterBo.getFollow();
        String membershipLevel = "无认证";

        if (userCenterBo.getBasicAuthentication().equals("1")) {
            membershipLevel = "已认证";
        }

        if (userCenterBo.getaManAuthentication().equals("1")) {
            membershipLevel = "达人认证";
        }

        if (userCenterBo.getBossAuthentication().equals("1")) {
            membershipLevel = "老板认证";
        }

        mBtrnMineSetting.setText(userCenterBo.getFollow() == 1 ? "已关注" : "关注");
        mTvUserName.setText(TextUtils.isEmpty(userCenterBo.getUserName()) ? "游客" : userCenterBo.getUserName());
        this.mData = userCenterBo;
        AM.image().bindToCircleObject(userCenterBo.getUserHead(), ivMineHead);
        mTvUserId.setText("ID:" + userCenterBo.getUserUid());
        mIvUserSex.setImageResource(userCenterBo.getSex() == 0 ? R.drawable.ic_man : R.drawable.ic_gender_women);
        mTvUserFans.setText("粉丝:" + userCenterBo.getFans());
        mTvUserAge.setText(userCenterBo.getAge() + "");
        mTvUserCertificationType.setText(membershipLevel);
        mTvUserCertificationPosition.setText(userCenterBo.getOccupation());
        mTvUserAddress.setText((TextUtils.isEmpty(userCenterBo.getCity()) ? "暂无地址" : userCenterBo.getCity())
                + (TextUtils.isEmpty(userCenterBo.getDetailAddress()) ? "" : userCenterBo.getDetailAddress()));
        mTvUserTime.setText("时间值：" + "￥" + userCenterBo.getTimeValue() + "/小时");
        mTvUserScore.setText("信用分：" + userCenterBo.getCreditScore());
        mTvUserComment.setText("评分：" + userCenterBo.getAssess());

        mTabEntities = new ArrayList<>();
        mFragments = new ArrayList<>();
        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i]));
        }

        mFragments.add(VedioOtherFragment.getInstance(userCenterBo, false, false));
        mFragments.add(PhotoFragment.getInstance(userCenterBo, false, false));
        tl6.setTabData(mTabEntities, this, R.id.fl_change, mFragments);
        tl6.setCurrentTab(1);
    }

    @Override
    public void showUploadHeadSuccess(String url) {
        showMsg(url);
        if (dialog != null) {
            dialog.dismiss();
        }
        AM.image().bindToCircleObject(url, ivMineHead);
    }

    @Override
    public void showFollowSuccess() {
        isFollow = 1;
        mBtrnMineSetting.setText("已关注");
        showMsg("关注成功！");
    }

    @Override
    public void showUnFollowSuccess() {
        isFollow = 0;
        mBtrnMineSetting.setText("关注");
        showMsg("已取消关注！");
    }

    @Override
    public void showError(String error) {
        showMsg(error);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
